<#assign pojo=oneToManyPojo.getOne()>
<#assign many=oneToManyPojo.getMany()>
<#assign ones=oneToManyPojo.getOnes()>
<#assign fields=pojo.getFilterdPojoFields()>
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>
<#assign moduleName = oneToManyPojo.getSystem()>
<#assign declarationNameFirstLetterLower = pojo.importType(pojo.getDeclarationNameFirstLetterLower())>
<#assign realPackageAndClassPath = "${pojo.getRealPackageAndClassPath()}">
<#assign masterid = pojo.getDeclarationNameFirstLetterLower()+"Id">
import request from '../../../core/utils/request';
import {isString} from '../../../../utils/utils'

const base_url = '/${moduleName}-api/${pojo.getDeclarationNameFirstLetterLower()}/';

export async function get${declarationName}ById (id) {
  return request({
    url: base_url + id,
    method: 'get',
    data: {id: id}
  })
}

export async function save${declarationName} (entity) {
if (entity !== null) {
    return request({
      url: base_url,
      method: 'post',
      data: entity
    })
  } else {
    throw new Error("保存的信息不能为空");
  }
}

export async function update${declarationName} (entity) {
if (entity !== null) {
if (entity.id !== null) {
      return request({
        url: base_url+entity.id,
        method: 'put',
        data: entity
      })
    }else{
      throw new Error("保存的信息id必须为空");
    }
  } else {
    throw new Error("保存的信息不能为空");
  }
}

export async function update${declarationName}Batch (keys,entity) {
  let ids_ = '';
  if (keys instanceof Array) {
    ids_ = keys.join(',');
} else if (isString(keys)  ) {
    ids_ = keys
  } else {
    throw new Error("不支持的主键类型");
  }
if (entity !== null) {
    return request({
      url: base_url + ids_,
      method: 'patch',
      data: entity
    })
  } else {
    throw new Error("保存的信息不能为空");
  }
}

export async function delete${declarationName} (entity) {
if (entity !== null) {
if (entity.id !== null) {
      return request({
        url: base_url + entity.id,
        method: 'delete'
      })
    } else {
      throw new Error("删除的数据还没有保存");
    }
  } else {
    throw new Error("保存的信息不能为空");
  }
}

export async function delete${declarationName}s (ids) {
if (ids !== null && ids.length > 0) {
    return request({
      url: base_url + ids,
      method: 'delete'
    })
  } else {
    throw new Error("保存的信息不能为空");
  }
}

export async function find${declarationName}ByList (params) {
  return request({
    url: base_url ,
    method: 'get',
    data: params
  })
}
export async function find${declarationName}ByPage (params) {
  return request({
    url: base_url + 'findByPage',
    method: 'get',
    data: params
  })
}

<#foreach child in many>
<#assign childdeclarationName = child.importType(child.getDeclarationName())>
<#assign childdeclarationNameFirstLetterLower = pojo.importType(child.getDeclarationNameFirstLetterLower())>
<#assign tempchild=child>

export async function save${childdeclarationName} (${declarationNameFirstLetterLower}Id,entity) {
if (entity !== null) {
      return request({
        url: base_url + '${childdeclarationNameFirstLetterLower}/'+${declarationNameFirstLetterLower}Id,
        method: 'post',
        data: entity
      })
  } else {
    throw new Error("保存的信息不能为空");
  }
}

export async function update${childdeclarationName} (${declarationNameFirstLetterLower}Id,entity) {
if (entity !== null) {
if (entity.id !== null) {
      return request({
        url: base_url + '${childdeclarationNameFirstLetterLower}/'+${declarationNameFirstLetterLower}Id+'/'+ entity.id,
        method: 'put',
        data: entity
      })
    }else{
        throw new Error("保存的信息id必须为空");
    }
  } else {
    throw new Error("保存的信息不能为空");
  }
}

export async function update${childdeclarationName}Batch (${declarationNameFirstLetterLower}Id,keys,entity) {
  let ids_ = '';
  if (keys instanceof Array) {
    ids_ = keys.join(',');
} else if (isString(keys)  ) {
    ids_ = keys
  } else {
    throw new Error("不支持的主键类型");
  }
if (entity !== null) {
    return request({
      url: base_url + '${childdeclarationNameFirstLetterLower}/'+${declarationNameFirstLetterLower}Id+'/'+ ids_,
      method: 'patch',
      data: entity
    })
  } else {
    throw new Error("保存的信息不能为空");
  }
}

export async function delete${childdeclarationName} (${declarationNameFirstLetterLower}Id,entity) {
if (entity !== null) {
if (entity.id !== null) {
      return request({
        url: base_url + '${childdeclarationNameFirstLetterLower}/'+${declarationNameFirstLetterLower}Id+'/'+ entity.id,
        method: 'delete'
      })
    } else {
      throw new Error("删除的数据还没有保存");
    }
  } else {
    throw new Error("保存的信息不能为空");
  }
}

export async function delete${childdeclarationName}s (${declarationNameFirstLetterLower}Id,ids) {
if (entity !== null && ids.length > 0) {
    return request({
      url: base_url + 'project/' + ${declarationNameFirstLetterLower}Id + '/' + ids,
      method: 'delete'
    })
  } else {
    throw new Error("保存的信息不能为空");
  }
}
<#if child.getRelation() == "MANY_DEFAULT">
export async function get${childdeclarationName}ById (${declarationNameFirstLetterLower}Id,id) {
  return request({
    url: base_url + '${childdeclarationNameFirstLetterLower}/'+${declarationNameFirstLetterLower}Id+'/'+  id,
    method: 'get',
    data: {id: id}
  })
}

export async function find${childdeclarationName}ByList (${declarationNameFirstLetterLower}Id,params) {
  return request({
    url: base_url + '${childdeclarationNameFirstLetterLower}/'+${declarationNameFirstLetterLower}Id,
    method: 'get',
    data: params
  })
}
export async function find${childdeclarationName}ByPage (${declarationNameFirstLetterLower}Id,params) {
  return request({
    url: base_url + '${childdeclarationNameFirstLetterLower}/'+${declarationNameFirstLetterLower}Id+ '/findByPage',
    method: 'get',
    data: params
  })
}
</#if>
</#foreach>
<#foreach child in ones>
</#foreach>